package com.shangcheng.server.service;


import com.shangcheng.dubbo.api.UmsResourceCategoryApi;
import com.shangcheng.model.domain.UmsResourceCategory;
import com.shangcheng.server.exception.BusinessException;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 后台资源分类管理Service实现类
 * Created by macro on 2020/2/5.
 */
@Service
public class UmsResourceCategoryService {
    @DubboReference
    private UmsResourceCategoryApi resourceCategoryApi;
    /**
     * 添加后台资源管理
     * @param umsResourceCategory
     */
    public void createResourceCategory(UmsResourceCategory umsResourceCategory) {
        UmsResourceCategory category= resourceCategoryApi.findResourceCategoryByName(umsResourceCategory);
        if (!ObjectUtils.isEmpty(category)){
            throw new BusinessException(category.getName()+"分类已存在");
        }
        resourceCategoryApi.createResourceCategory(umsResourceCategory);
    }

    /**
     * 根据id删除后台资源分类
     * @param id
     */
    public void deleteResourceCategory(Integer id) {
        resourceCategoryApi.deleteResourceCategory(id);
    }

    /**
     * 查询所有后台资源分类
     * @return
     */
    public List<UmsResourceCategory> resourceCategoryList() {
        return resourceCategoryApi.resourceCategoryList();
    }

    /**
     *修改后台资源分类
     * @param umsResourceCategory
     */
    public void updateResourceCategory(UmsResourceCategory umsResourceCategory) {
        UmsResourceCategory category = resourceCategoryApi.findResourceCategoryByName(umsResourceCategory);
        if (!ObjectUtils.isEmpty(category)){
            if (!ObjectUtils.nullSafeEquals(umsResourceCategory.getId(),category.getId())){
                throw new BusinessException(category.getName()+"分类存在");
            }
        }
        resourceCategoryApi.updateResourceCategory(umsResourceCategory);
    }
}
